<?php
/**
 * Created by PhpStorm.
 * Script Name: BotReply.php
 * Create: 2023/2/13 7:43
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace plugin\wechat\app\service;


use app\common\service\Media;
use plugin\wechat\extend\WxBot\Driver\Cat;
use plugin\wechat\extend\WxBot\Driver\My;
use plugin\wechat\extend\WxBot\Driver\Mycom;
use plugin\wechat\extend\WxBot\Driver\Qianxun;
use plugin\wechat\extend\WxBot\Driver\Vlw;
use plugin\wechat\extend\WxBot\Driver\Wxwork;
use plugin\wechat\app\model\BotReply as ReplyM;

class BotReply extends Common
{
    const BEADDED = 'beadded';
    const BEINVITED = 'beinvited';
    const FRIEND_IN = 'friend_in';
    const MSG = 'msg';

    const HANDLE_RM = 'rm';
    const HANDLE_DEL = 'del';
    const HANDLE_MSG = 'msg';

    public static function events($id = null){
        $list = [
            self::BEADDED => '被加好友',
            self::FRIEND_IN => '新人入群',
            self::BEINVITED => '被邀请入群',
            self::MSG => '消息事件'
        ];
        return isset($list[$id]) ? $list[$id] : $list;
    }

    public static function handleTypes($id = null){
        $list = [
            self::HANDLE_MSG => '消息回复',
            self::HANDLE_DEL => '删除好友',
            self::HANDLE_RM => '移出群聊',
        ];
        return isset($list[$id]) ? $list[$id] : $list;
    }

    /**
     * 动作响应类型
     * @param null $id
     * @return array|mixed
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function actionTypes($id = null){
        $list = self::handleTypes();
        unset($list[self::HANDLE_MSG]);
        return isset($list[$id]) ? $list[$id] : $list;
    }

    /**
     * 获取回复
     * @param array $where
     * @param bool $refresh
     * @return array|mixed|\think\Collection|\think\db\Query[]
     * @throws \Psr\SimpleCache\InvalidArgumentException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    static function getReplies($where = [], $refresh = false){
        $cache_key = wechat_cache_key(serialize($where));
        $data = cache($cache_key);
        if(empty($data) || $refresh){
            $data = ReplyM::where($where)
                ->order(['sort' => 'desc'])
                ->select();
        }
        cache($cache_key, $data);
        return $data;
    }

    /**
     * 封装素材回复
     * @param $bot
     * @param $client Vlw|Wxwork|Cat|My|Mycom|Qianxun
     * @param $reply
     * @param string $to_wxid
     * @param array $extra
     * Author: fudaoji<fdj@kuryun.cn>
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    static function botReply($bot, $client, $reply, $to_wxid = '', $extra = []){
        $media = Media::getModel($reply['media_type'])->where('company_id' ,$bot['company_id'])
        ->find($reply['media_id']);

        switch($reply['media_type']){
            case Media::LINK:
                $client->sendShareLinkToFriends([
                    'robot_wxid' => $bot['uin'],
                    'uuid' => $bot['uuid'],
                    'to_wxid' => $to_wxid,
                    'url' => $media['url'],
                    'image_url' => $media['image_url'],
                    'title' => $media['title'],
                    'desc' => $media['desc']
                ]);
                break;
            case Media::TEXT:
                $msg = empty($extra['nickname']) ? $media['content'] : str_replace('[昵称]', $extra['nickname'], $media['content']);
                if(!empty($extra['need_at'])){
                    $client->sendGroupMsgAndAt(['robot_wxid' => $bot['uin'], 'group_wxid' => $to_wxid, 'member_wxid' => $extra['member_wxid'], 'msg' => $msg]);
                }elseif(!empty($extra['atall'])){
                    $group_ids = explode(',', $to_wxid);
                    foreach ($group_ids as $gid){
                        $client->sendMsgAtAll(['robot_wxid' => $bot['uin'], 'group_wxid' => $gid, 'msg' => $msg]);
                    }
                }else{
                    $client->sendTextToFriends(['robot_wxid' => $bot['uin'], 'uuid' => $bot['uuid'], 'to_wxid' => $to_wxid, 'msg' => $msg]);
                }
                break;
            case Media::IMAGE:
                $client->sendImgToFriends([
                    'robot_wxid' => $bot['uin'],
                    'uuid' => $bot['uuid'],
                    'to_wxid' => $to_wxid,
                    'path' => $media['url']
                ]);
                break;
            case Media::VIDEO:
                $client->sendVideoToFriends([
                    'robot_wxid' => $bot['uin'],
                    'uuid' => $bot['uuid'],
                    'to_wxid' => $to_wxid,
                    'path' => $media['url']
                ]);
                break;
            case Media::FILE:
                $client->sendFileToFriends([
                    'robot_wxid' => $bot['uin'],
                    'uuid' => $bot['uuid'],
                    'to_wxid' => $to_wxid,
                    'path' => $media['url']
                ]);
                break;
        }
    }
}